package com.xiezc.action;

import com.alibaba.fastjson.JSON;
import com.xiaoleilu.hutool.lang.Assert;
import com.xiezc.entity.FileInfo;
import com.xiezc.util.RetrofitUtil;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import org.springframework.stereotype.Component;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;

/**
 * Created by wb-xzc291800 on 2017/6/22.
 */
@Component
public class DownloadAction {

    DownloadInterface downloadInterface = RetrofitUtil.create(DownloadInterface.class);


    public void downloadFile(String ticket, FileInfo fileInfo ,Callback callback) {
        Assert.notBlank(ticket);
        Assert.notNull(fileInfo);
        RequestBody requestBody = RequestBody
                .create(
                        MediaType.parse("application/json; charset=utf-8"),
                        JSON.toJSON(fileInfo).toString()
                );
        downloadInterface.downloadFile(ticket, requestBody).enqueue(callback);
    }


    interface DownloadInterface {

        @POST("download/downloadFile")
        Call<ResponseBody> downloadFile(@Header("ticket") String ticket, @Body RequestBody requestBody);
    }
}
